package com.luoying.itew.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.luoying.itew.R;
import com.luoying.itew.util.LogUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/24.
 * time 2018-5-10
 * author zm
 */

public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {
    /**
     * loaing对话框
     */
    protected ProgressDialog loadDialog;
    protected T viewBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String tag = getClass().getSimpleName();
        long t1 = System.currentTimeMillis();
        viewBindingInit();
        LogUtils.e(tag,"t1:"+(System.currentTimeMillis()-t1));
        t1 = System.currentTimeMillis();
        if(viewBinding==null){
            setContentView(getLayoutResId());
        } else {
            setContentView(viewBinding.getRoot());
        }
        LogUtils.e(tag,"t2:"+(System.currentTimeMillis()-t1));
        t1 = System.currentTimeMillis();
        ButterKnife.bind(this);
        getCreateBundle(savedInstanceState);
        init();
        InitActionBar();
        LogUtils.e(tag,"t3:"+(System.currentTimeMillis()-t1));
        setSystemColor();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  //禁止横屏
    }

    public abstract int getLayoutResId();

    public BaseViewModel getBaseViewModel(BaseViewModel baseViewModel){
        return  baseViewModel;
    }

    /**
     * 通过反射生成ViewBinding对象
     * 有两种方式绑定布局ButterKnife 和viewBinding你可以自由选择 如果使用ButterKnife可以不指定ViewBinding写法参考MainActivity
     * 推荐使用viewBinding会方便点
     */
    private void viewBindingInit(){
        long start = System.currentTimeMillis();
        Type type =getClass().getGenericSuperclass();
        if(type instanceof ParameterizedType){
            try {
                Class<T> temp = (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[0];
                Method method = temp.getMethod("inflate",LayoutInflater.class);
                viewBinding = (T) method.invoke(null,LayoutInflater.from(this));
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        LogUtils.e("viewBindingInit time :"+(end-start));
    }



    protected void init() {
        loadDialog = new ProgressDialog(this);
        loadDialog.setMessage(getString(R.string.load_ing));
        loadDialog.setCanceledOnTouchOutside(false);
    }
    /**
     *
     * 关联ActionBar的一些布局，并设置一些点击事件
     */
    protected void InitActionBar() {

    }
    /**
     * 设置系统状态栏颜色以及调整ActionBar的高度
     */
    protected void setSystemColor() {

    }

    /**
     * 获取创建时的状态
     * @param savedInstanceState
     */
    protected void getCreateBundle(Bundle savedInstanceState){

    }

    public void toWithActivity(Class activityClass,Bundle bundle){
        Intent intent  = new Intent(this,activityClass);
        if(bundle!=null)
            intent.putExtras(bundle);
        startActivity(intent);

    }

    public void toWithActivity(Class activityClass){
        toWithActivity(activityClass,null);
    }

    public void JumpToWithAni(Class activity){
        Intent intent = new Intent(this,activity);
        startActivity(intent);
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
    public void JumpTo(Class activity){
        Intent intent = new Intent(this,activity);
        startActivity(intent);
        finish();
    }
    public void ClimbToWithAni(Class activity){
        Intent intent = new Intent(this,activity);
        startActivity(intent);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
    public void ClimbTo(Class activity){
        Intent intent = new Intent(this,activity);
        startActivity(intent);
    }
}
